package BTree;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 故我
 * Date: 2024-02-03
 * Time: 12:36
 */
public class BinaryTree {
    static class TreeNode {
        private int val;
        private TreeNode left;
        private TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }

    private TreeNode root;

    private int nodeSize;

    public TreeNode createTree() {
        TreeNode A = new TreeNode(1);
        TreeNode B = new TreeNode(2);
        TreeNode C = new TreeNode(3);
        TreeNode D = new TreeNode(4);
        TreeNode E = new TreeNode(5);
        TreeNode F = new TreeNode(6);
        TreeNode G = new TreeNode(7);
        TreeNode H = new TreeNode(8);
        TreeNode I = new TreeNode(9);

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        H.left = I;

        return A;
    }

    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if (root == null) {
            return null;
        }
        list.add(root.val);
        List<Integer> leftlist = preorderTraversal(root.left);
        list.addAll(leftlist);
        List<Integer> rightlist = preorderTraversal(root.right);
        list.addAll(rightlist);

        return list;
    }

    /**
     * 求树的节点个数
     */
    public int size(TreeNode root) {
        if (root == null) {
            return 0;
        }

        return size(root.left) + size(root.right) + 1;
//        nodeSize++;
//        size(root.left);
//        size(root.right);
//
//        return nodeSize;
    }

    public int getLeafSize(TreeNode root) {
        if (root == null) {
            return 0;
        }

        if (root.left == null && root.right == null) {
            return 1;
        }

        return getLeafSize(root.left) + getLeafSize(root.right);
    }

    public int getKLevelNodeCount(TreeNode root, int k) {
        if (root == null) {
            return 0;
        }

        if (k == 1) {
            return 1;
        }

        return getKLevelNodeCount(root.left, k - 1) + getKLevelNodeCount(root.right, k - 1);
    }


    // 获取树的高度
    public int getTreeHight(TreeNode root) {
        if (root == null) {
            return 0;
        }

        int leftHight = getTreeHight(root.left);
        int rightHight = getTreeHight(root.right);

        return Math.max(leftHight + 1, rightHight + 1);
    }

    // 判断这棵是否含有key
    public boolean isHasTreeKey(TreeNode root, int key) {
        if (root == null) {
            return false;
        }

        if (root.val == key) {
            return true;
        }

        return isHasTreeKey(root.left, key)
                || isHasTreeKey(root.right, key);
    }

    // 层序遍历
    public void levelOrder(TreeNode root) {
        if (root == null) {
            return;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();// 记录出队头元素
            System.out.print(cur.val + " ");

            if (cur.left != null) {
                queue.offer(cur.left);
            }

            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }

    // 前序遍历 非递归
    public void PreOrderNo(TreeNode root) {
        if (root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        // 右子树为空时，就判断栈是否为空
        while (cur != null || !stack.empty()) {
            while (cur != null) {
                stack.push(cur);
                System.out.print(cur.val + " ");
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            cur = top.right;
        }
    }

    // 中序遍历 非递归
    public void InOrderNo(TreeNode root) {
        if (root == null) {
            return;
        }

        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.empty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }

            TreeNode top = stack.pop();
            System.out.print(top.val + " ");
            cur = cur.right;
        }
    }

    // 后序遍历  非递归
    public void PostOrderNo(TreeNode root) {
        if (root == null) {
            return;
        }

        TreeNode cur = root;
        TreeNode prev = null;
        Stack<TreeNode> stack = new Stack<>();
        while (cur != null) {
            stack.push(cur);
            cur = cur.left;
        }

        // 瞄一眼栈顶元素的右子树是否为空   还需要判断右子树是否已经遍历过了
        TreeNode top = stack.peek();
        if (top.right == null || top.right == prev) {
            System.out.print(top.val + " ");
            stack.pop();
            prev = top; // 记录这个打印过的节点
        } else {
            // 此时右子树不为空情况
            cur = top.right;
        }
    }

}
